package com.baizhi.config;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baizhi.filter.AccessControlAllowFilter;

import com.baizhi.intercepter.MyInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        // 设置请求的页面大于最大页后操作， true调回到首页， 默认false
        paginationInterceptor.setOverflow(true);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInterceptor.setLimit(500);
        return paginationInterceptor;
    }
    @Bean
    public FilterRegistrationBean filterRegist() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter(new AccessControlAllowFilter());
        frBean.addUrlPatterns("/*");
        return frBean;
    }
        //拦截器
//        public void addInterceptors(InterceptorRegistry registry) {
//            //添加MyInterceptor到注册机里面，该拦截器拦截所有的请求，排除掉/user/*的请求
//            registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**")
//                    .excludePathPatterns("/user/**");
//
//    }

    // Endpoint以北京为例。
    public  static String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    @Bean
    public OSS ossClient(){

        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        String accessKeyId="LTAI5tPsLc7rrYsarxWMdS24";
        String accessKeySecret = "VOD5p01WuocgICEGtrVW8freTY0Ew7";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+ENDPOINT, accessKeyId, accessKeySecret);
        return ossClient;

    }


}
